<?php

class DepartmentClass extends Framework
{
	/**
	 * 构造检索条件
	 * @author workflow Develop Team
	 * @param  
	 * @return str
	 */
	function _conditionSearch($obj_condition){
		$conditionStr = "";

		if ($obj_condition[id] != ""){/*部门iD*/
			$conditionStr .= " and id = '" . $obj_condition[id] . "'";
		}
		if ($obj_condition['name'] != ""){//部门名称
			$conditionStr .= " and name = '" . $obj_condition['name'] . "'";
		}
		/* 父id */
		if ($obj_condition['parent_id'] != '')
		{
			$conditionStr .= " and parent_id = '" . $obj_condition['parent_id'] . "'";
		}
		/* 状态 */
		if ($obj_condition['status'] != '')
		{
		    $conditionStr .= " and status = '" . $obj_condition['status'] . "'";
		}
		if ($obj_condition[order] == 1){/*时间倒叙排序*/
			$conditionStr .=" order by create_time desc";
		}
		return $conditionStr;
	}
	
	/*
	 * 添加部门信息
	 *
	 **/
	public function addDepartment($input)
	{
		$value_array = array();
		$value_array['parent_id'] = $input['parent_id'] ? $input['parent_id'] : 0;
		$value_array['name']      = $input['sys_department_name'];
		$value_array['status']     = 1;
		$value_array['create_time'] = time();
		$value_array['create_user_id'] = $_SESSION['s_admin']['id'];

		if ($GLOBALS['db']->insertRow($value_array, 'department', 'id'))
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	/*
	 * 部门信息列表
	 *
	 **/
	public function getDepartmentList($obj_condition, $obj_page)
	{
		/**
		 * 构造查询条件
		 */
		$condition_str = $this->_conditionSearch($obj_condition);
		$sys_department_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "department");
		return $sys_department_array;
	
	}

	/*
	 * 根据id获取部门信息
	 *
	 **/
	public function getDepartmentById($id)
	{
	    $array = $GLOBALS['db']->GetOneRow($id,'department','id');
		return $array;
	}

	/*
	 * 更新部门信息
	 *
	 **/
	public function updateDepartment($input)
	{
	    $id = $input['id'];
		if (intval($id) > 0){
		    $value_array = array();
		    $value_array['name'] = $input['sys_department_name'];
		    $value_array['parent_id'] = $input['parent_id'] ? $input['parent_id'] : 0;
		    $value_array['status'] = $input['status'];
			$rs = $GLOBALS['db']->UpdateRow(intval($id),$value_array,'department','id');
			return $rs;
		}else {
			return false;
		}
	}
	/**
	 * 删除部门信息
	 * @author workflow Develop Team
	 * @param  $input 删除的部门ID数组
	 * @return bool
	 */
	function delDepartment($input){
		if (intval($input) > 0){
			return $GLOBALS['db']->DelRow($input,'department','id');
		}else {
			return false;
		}
	}
}

?>